package com.ali.springcloud;

import com.ali.springcloud.service.PaymentHystirxService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：ALi
 * @date ：Created in 2022/7/4 15:02
 * @description：控制类
 * @modified By：
 * @version: $
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")// hystrix 全局fallback方法
public class OrderHystirxController {

    @Autowired
    private PaymentHystirxService paymentHystirxService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String infoOk = paymentHystirxService.paymentInfo_OK(id);
        return infoOk;
    }
    //降级
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "6000")
//    })
    @HystrixCommand()//什么也不加默认使用全局的
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
        String result = paymentHystirxService.paymentInfoTimeOut(id);
        return result;
    }
    public String paymentInfo_TimeOutHandler(Integer id) {

        return "线程池:  " + Thread.currentThread().getName() + " paymentInfo_TimeOutHandler,id:"+id+"系统繁忙";

    }

    /**
     * hystrix 全局fallback方法
     *
     * @return
     */
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试，/(ㄒoㄒ)/~~";
    }
}